package com.wanda.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonModelGeneral;

import com.wanda.model.Role_MenuModel;
import com.wanda.model.User_functionModel;
import com.wanda.service.Role_MenuService;


@Controller
@RequestMapping(value="/qt/Role_Menu")
public class Role_MenuController {

	@Resource
	private Role_MenuService role_MenuService;
	
	@RequestMapping(value="/addRole_Menu.do")
    public @ResponseBody Map<String,Object> add(Role_MenuModel Role){
		Role.setDelete_state("0");
		Role.setDelete_time("19960202");
		Role.setDelete_user("saf");
		Role.setMenu_id("asdf6");
		Role.setRole_menu_id("asdef");
		Role.setUser_id("asdf");
		Role.setUpdate_time("16660606");
		Role.setUpdate_user("sadfasf");
		role_MenuService.addRole_Menu(Role);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	@RequestMapping(value="/deleteRole_Menu.do")
	 public @ResponseBody Map<String,Object> update(Role_MenuModel Role){
		Role.setRole_menu_id("123");
		Map<String,Object> map = new HashMap<String,Object>(); 
		role_MenuService.deleteRole_Menu(Role);
    	map.put("msg","成功");
		return map;
	}
	
	@RequestMapping(value="/selectAll.do")
   public @ResponseBody JsonModelGeneral select(){
		JsonModelGeneral json = new JsonModelGeneral();
		json.setRows(role_MenuService.selectAll());
		return json;
	}
	
	@RequestMapping(value="/selectById.do")
   public @ResponseBody JsonModelGeneral selectbyid(String id){
		JsonModelGeneral json = new JsonModelGeneral();
		json.setRows(role_MenuService.selectById("123"));
		return json;
	}

	@RequestMapping(value="/updateRole_Menu.do")
   public @ResponseBody Map<String,Object> updateOrganization(HttpServletRequest request,HttpServletResponse response){
    	
		
		String xx =request.getParameter("ids");
		String str[]=xx.split(",");  
		
		String id= request.getParameter("use");
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+id+"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		String info="";
		if(id!=null){
		int c=	role_MenuService.deleteByUserId(id);
	      if(c>0){
	    	   info="更新成功 需要重新登录";
	      }else{
	    	  info="失败   更新数等于0";
	      }
		 
		  if(str!=null){
			  for(String st:str){
				  Role_MenuModel model = new Role_MenuModel();
				  model.setUser_id(id);
				  model.setMenu_id(st);
				  role_MenuService.addRole_Menu(model);
			  }
		  }else{
			  info="失败 权限id为null";
			//  System.out.println(); 
		  }
	   }else{
		  info="失败 用户id为null";
		   //System.out.println();
	   }
		
		 PrintWriter out = null;
//		try {
//		//	out = response.getWriter();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
 	     // String info ="<script>if(window.confirm('您没有权限')){window.history.back(0);}else{window.history.back(0);}</script>";	
 	     String info1="<script>alert('"+info+"');</script>";
 	   //  out.write(info1);
		
		
//		System.out.println("=======================================");
    	Map<String,Object> map = new HashMap<String,Object>(); 
//		Role.setDelete_time("19960202");
//		Role.setDelete_user("987");
//		Role.setMenu_id("asdf6");
//		Role.setRole_menu_id("123");
//		Role.setUser_id("987");
//		Role.setUpdate_time("16660606");
//		Role.setUpdate_user("sadfasf");
//		role_MenuService.updateRole_Menu(Role);
		map.put("msg","成功");
		return map;
	} 
	
	@RequestMapping(value="/selectByUser_id.do")
	   public @ResponseBody JsonModelGeneral selectByUser_id(String User_id){
	
		JsonModelGeneral json = new JsonModelGeneral();
		json.setRows(role_MenuService.selectByUser_id(User_id));
		
			return json;
	} 
	
	
}
